package kr.or.kosta.otr.utill;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.or.kosta.otr.vo.CompanyVO;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class ExcelView extends AbstractExcelView{
	@Override
	protected void buildExcelDocument(Map<String,Object> ModelMap,HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception{
		System.out.println("---- ExcelDownloadView ----");
		String excelName = "file";
		HSSFSheet worksheet = null;
		HSSFRow row = null;
		excelName=URLEncoder.encode("file","euc-kr");
		worksheet = workbook.createSheet(excelName+ " WorkSheet");
		ArrayList<CompanyVO> cList = (ArrayList<CompanyVO>) ModelMap.get("cList");
		row = worksheet.createRow(0);
		row.createCell(0).setCellValue("회사명");
		row.createCell(1).setCellValue("산업");
		row.createCell(2).setCellValue("지역");
		row.createCell(3).setCellValue("기업대표자");
		row.createCell(4).setCellValue("기업매출액");
		row.createCell(5).setCellValue("회사소개");
		row.createCell(6).setCellValue("연봉");
		row.createCell(7).setCellValue("Url");
		row.createCell(8).setCellValue("기업총점");
		
		for(int i=1; i<cList.size()+1;i++){
			row = worksheet.createRow(i);
			row.createCell(0).setCellValue(cList.get(i-1).getCompanyName());
			row.createCell(1).setCellValue(cList.get(i-1).getIndustryVo().getIndustryName());
			row.createCell(2).setCellValue(cList.get(i-1).getRegionVo().getRegionName());
			row.createCell(3).setCellValue(cList.get(i-1).getCompanyPresident());
			row.createCell(4).setCellValue(cList.get(i-1).getCompanySales());
			row.createCell(5).setCellValue(cList.get(i-1).getCompanyInfo());
			row.createCell(6).setCellValue(cList.get(i-1).getCompanySalary());
			row.createCell(7).setCellValue(cList.get(i-1).getCompanyUrl());
			row.createCell(8).setCellValue(cList.get(i-1).getCompanyTotalScore());
		}
		
		response.setContentType("Application/Msexcel");
		response.setHeader("Content-Disposition", "Attachment; Filename=\"" + excelName + ".xls");
		response.setHeader("Content-Transfer-Encoding", "binary");
	}
}
